package org.wenhua.yuan

import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.activity_main.*
import org.wenhua.yuan.and.fragment.*
import org.wenhua.yuan.and.util.ImmersiveStatusBar
import org.wenhua.yuan.and.views.tab.TabBtnBottomInfo
import org.wenhua.yuan.and.views.tab_fragment.TabBtnFragmentAdapter
import java.util.*


class MainActivity : AppCompatActivity() {
    var tabInfoList: MutableList<TabBtnBottomInfo<*>> = ArrayList()
    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersiveStatusBar.setStatusBar(this, true,
            resources.getColor(R.color.colorCMBCPrimaryLight), false)
        setContentView(R.layout.activity_main)


        var tabBottomCMBCNormal = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_cmbc_normal,
            BitmapFactory.Options()
        )
        var tabBottomCMBCSelected = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_cmbc_selected,
            BitmapFactory.Options()
        )
        var tabBottomIMNormal = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_im_normal,
            BitmapFactory.Options()
        )
        var tabBottomIMSelected = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_im_selected,
            BitmapFactory.Options()
        )
        var tabBottomICBCNormal = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_icbc_normal,
            BitmapFactory.Options()
        )
        var tabBottomICBCSelected = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_icbc_selected,
            BitmapFactory.Options()
        )
        var tabBottomDouNormal = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_dou_normal,
            BitmapFactory.Options()
        )
        var tabBottomDouSelected = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_dou_selected,
            BitmapFactory.Options()
        )
        var tabBottomVIPNormal = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_vip_normal,
            BitmapFactory.Options()
        )
        var tabBottomVIPSelected = BitmapFactory.decodeResource(
            resources,
            R.mipmap.ic_vip_selected,
            BitmapFactory.Options()
        )

        var tabBottomCMBC = TabBtnBottomInfo<Int>(
            "招行",
            tabBottomCMBCNormal,
            tabBottomCMBCSelected,
            resources.getColor(R.color.colorTabSelected),
            resources.getColor(R.color.colorTabDefault)
        )

        var tabBottomIM = TabBtnBottomInfo<Int>(
            "即时通讯",
            tabBottomIMNormal,
            tabBottomIMSelected,
            resources.getColor(R.color.colorTabSelected),
            resources.getColor(R.color.colorTabDefault)
        )
        var tabBottomICBC = TabBtnBottomInfo<Int>(
            "工行",
            tabBottomICBCNormal,
            tabBottomICBCSelected,
            resources.getColor(R.color.colorTabSelected),
            resources.getColor(R.color.colorTabDefault)
        )
        var tabBottomDou = TabBtnBottomInfo<Int>(
            "抖音",
            tabBottomDouNormal,
            tabBottomDouSelected,
            resources.getColor(R.color.colorTabSelected),
            resources.getColor(R.color.colorTabDefault)
        )

        var tabBottomVIP = TabBtnBottomInfo<Int>(
            "唯品会",
            tabBottomVIPNormal,
            tabBottomVIPSelected,
            resources.getColor(R.color.colorTabSelected),
            resources.getColor(R.color.colorTabDefault)
        )

        tabBottomCMBC.fragment = CMBCTabFragment::class.java
        tabBottomIM.fragment = IMTabFragment::class.java
        tabBottomICBC.fragment = ICBCTabFragment::class.java
        tabBottomDou.fragment = DOUTabFragment::class.java
        tabBottomVIP.fragment = VIPTabFragment::class.java
        tabInfoList.add(tabBottomCMBC)
        tabInfoList.add(tabBottomIM)
        tabInfoList.add(tabBottomICBC)
        tabInfoList.add(tabBottomDou)
        tabInfoList.add(tabBottomVIP)
        tabBtnLayoutNav.inflateInfo(tabInfoList)
        tabBtnLayoutNav.alpha = 1f
        tabBtnLayoutNav.defaultSelected(tabBottomCMBC)
//        var tabBtnBottom:TabBtnBottom? = tabBtnLayoutNav.findTab(tabBottomICBC)
//        tabBtnBottom?.resizeHeight(60) // 调整大小，可以使中间按钮显示凸出效果

        var fragmentAdapter = TabBtnFragmentAdapter(supportFragmentManager, tabInfoList)
        var fragmentTabLayout = tabBtnLayoutNav.tabBtnFragmentLayout
        fragmentTabLayout.setAdapter(fragmentAdapter)

        tabBtnLayoutNav.addTabSelectedChangeListener { index, curTabInfo, nextTabInfo ->
            fragmentTabLayout.setCurrentItem(index)
        }

        test() // 测试使用

    }



    fun test() {
        val a: MutableLiveData<String> = MutableLiveData<String>()

        a.observe(this, object : LifecycleEventObserver, Observer<String> {

            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                Log.e("CMBCDataFeatureArea1", event.name)
            }

            override fun onChanged(t: String?) {
                Log.e("CMBCDataFeatureArea2", t.toString())
            }
        })


        a.postValue("livedata感知宿主生命周期，订阅/发布")
    }


}

